package com.zhouixi.repository;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
public class SessionManager {
    private final ConcurrentHashMap<Long, WebSocketSession> sessions = new ConcurrentHashMap<>();

    public void addSession(Long userId, WebSocketSession session) {
        sessions.put(userId, session);
    }

    public WebSocketSession getSession(Long userId) {
        return sessions.get(userId);
    }

    public void removeSession(Long userId) {
        WebSocketSession session = this.getSession(userId);
        if (session != null) {
            try {
                sessions.remove(userId);
            } catch (Exception e) {
                log.error("removeSession error", e);
            }
        }
    }
}
